package com.szboanda.mpgz.utill;


import org.bouncycastle.crypto.digests.SM3Digest;
import org.bouncycastle.crypto.macs.HMac;
import org.bouncycastle.crypto.params.KeyParameter;
import org.bouncycastle.util.encoders.Hex;

import java.io.UnsupportedEncodingException;

public class SM3Utils {
    /**
     * SM3值的长度
     */
    private static final int BYTE_LENGTH = 32;
    /**
     * SM3分组长度
     */
    private static final int BLOCK_LENGTH = 64;
    /**
     * 缓冲区长度
     */
    private static final int BUFFER_LENGTH = BLOCK_LENGTH * 1;
    /**
     * 缓冲区
     */
    private byte[] xBuf = new byte[BUFFER_LENGTH];
    /**
     * 缓冲区偏移量
     */
    private int xBufOff;

    public static String hmac(String key, String src) throws UnsupportedEncodingException {
        byte[] keyData = key.getBytes("UTF-8");
        byte[] srcData = src.getBytes("UTF-8");
        KeyParameter keyParameter = new KeyParameter(keyData);
        SM3Digest digest = new SM3Digest();
        HMac mac = new HMac(digest);
        mac.init(keyParameter);
        mac.update(srcData, 0, srcData.length);
        byte[] result = new byte[mac.getMacSize()];
        mac.doFinal(result, 0);
        return Hex.toHexString(result);
    }

}

